#include "orderfactory.h"

NormalOrder* OrderFactory::createBuyOrder (time_t time, const char *symbol, double price, double lots, double stopLoss, double takeProfit, int magicNumber)
{
    NormalOrder *pOrder = new NormalOrder();
    pOrder->m_id = pOrder->getNewNo();
    pOrder->m_openTime = time;
    pOrder->m_currTime = time;
    strcpy(pOrder->m_symbol, symbol);
    pOrder->setOrderType(OrderBase::ORDERBUY);
    pOrder->m_openPrice = price;
    pOrder->m_currPrice = price;
    pOrder->m_highPrice = pOrder->m_lowPrice = price;
    pOrder->m_lots = lots;
    pOrder->m_stopLoss = stopLoss;
    pOrder->m_takeProfit = takeProfit;
    pOrder->m_leverageRate = 50;
    pOrder->m_fee = 0.0004;
    return pOrder;
}

NormalOrder* OrderFactory::createSellOrder(time_t time, const char *symbol, double price, double lots, double stopLoss, double takeProfit, int magicNumber)
{
    NormalOrder *pOrder = new NormalOrder();
    pOrder->m_id = pOrder->getNewNo();
    pOrder->m_openTime = time;
    pOrder->m_currTime = time;
    strcpy(pOrder->m_symbol, symbol);
    pOrder->setOrderType(OrderBase::ORDERSELL);
    pOrder->m_openPrice = price;
    pOrder->m_currPrice = price;
    pOrder->m_highPrice = pOrder->m_lowPrice = price;
    pOrder->m_lots = lots;
    pOrder->m_stopLoss = stopLoss;
    pOrder->m_takeProfit = takeProfit;
    pOrder->m_leverageRate = 50;
    pOrder->m_fee = 0.0004;
    return pOrder;
}

PendingOrder* OrderFactory::createBuyLimitOrder (time_t time, const char *symbol, double price, double lots, double stopLoss, double takeProfit, int magicNumber)
{
    PendingOrder *pOrder = new PendingOrder();
    pOrder->m_id = pOrder->getNewNo();
    pOrder->m_currTime = time;
    pOrder->m_openTime = time;
    strcpy(pOrder->m_symbol, symbol);
    pOrder->setOrderType(OrderBase::ORDERBUYLIMIT);
    pOrder->m_openPrice = price;
    pOrder->m_currPrice = price;
    pOrder->m_lots = lots;
    pOrder->m_stopLoss = stopLoss;
    pOrder->m_takeProfit = takeProfit;
    pOrder->m_leverageRate = 50;
    pOrder->m_fee = 0.0004;
    return pOrder;
}

PendingOrder* OrderFactory::createSellLimitOrder(time_t time, const char *symbol, double price, double lots, double stopLoss, double takeProfit, int magicNumber)
{
    PendingOrder *pOrder = new PendingOrder();
    pOrder->m_id = pOrder->getNewNo();
    pOrder->m_currTime = time;
    pOrder->m_openTime = time;
    strcpy(pOrder->m_symbol, symbol);
    pOrder->setOrderType(OrderBase::ORDERSELLLIMIT);
    pOrder->m_openPrice = price;
    pOrder->m_currPrice = price;
    pOrder->m_lots = lots;
    pOrder->m_stopLoss = stopLoss;
    pOrder->m_takeProfit = takeProfit;
    pOrder->m_leverageRate = 50;
    pOrder->m_fee = 0.0004;
    return pOrder;
}

HistoryOrder* OrderFactory::createHistoryOrder(const char *strOrder)
{
    HistoryOrder *pOrder = new HistoryOrder();
    pOrder->m_id = pOrder->getNewNo();
    pOrder->m_leverageRate = 50;
    pOrder->m_fee = 0.0004;
    pOrder->m_stopLoss = 0;
    pOrder->m_takeProfit = 0;

    const char *pos;
    int tmpTime;
    char tmpStr[100];
    pos = strstr(strOrder, "Symbol: ");
    if (pos) {
        pos += strlen("Symbol: ");
        sscanf(pos, "%s", pOrder->m_symbol);
    }

    pos = strstr(strOrder, "Type: ");
    if (pos) {
        pos += strlen("Type: ");
        sscanf(pos, "%s", tmpStr);
        changeOrderType(pOrder, tmpStr);
    }

    pos = strstr(strOrder, "OrderLots: ");
    if (pos) {
        pos += strlen("OrderLots: ");
        sscanf(pos, "%lf", &(pOrder->m_lots));
    }

    pos = strstr(strOrder, "TimetO: ");
    if (pos) {
        pos += strlen("TimetO: ");
        sscanf(pos, "%d", &(tmpTime));
        pOrder->m_openTime = tmpTime;
    }

    pos = strstr(strOrder, "TimetC: ");
    if (pos) {
        pos += strlen("TimetC: ");
        sscanf(pos, "%d", &(tmpTime));
        pOrder->m_closeTime = pOrder->m_currTime = tmpTime;
    }

    pos = strstr(strOrder, "OpenPrice: ");
    if (pos) {
        pos += strlen("OpenPrice: ");
        sscanf(pos, "%lf", &(pOrder->m_openPrice));
    }

    pos = strstr(strOrder, "ClosePrice: ");
    if (pos) {
        pos += strlen("ClosePrice: ");
        sscanf(pos, "%lf", &(pOrder->m_closePrice));
        pOrder->m_currPrice = pOrder->m_closePrice;
    }

    pos = strstr(strOrder, "HighPrice: ");
    if (pos) {
        pos += strlen("HighPrice: ");
        sscanf(pos, "%lf", &(pOrder->m_highPrice));
    }

    pos = strstr(strOrder, "LowPrice: ");
    if (pos) {
        pos += strlen("LowPrice: ");
        sscanf(pos, "%lf", &(pOrder->m_lowPrice));
    }

    return pOrder;
}

HistoryOrder* OrderFactory::createHistoryOrder(NormalOrder *normalOrder)
{
    HistoryOrder *pOrder = new HistoryOrder();
    pOrder->m_id = normalOrder->m_id;
    strcpy(pOrder->m_symbol, normalOrder->m_symbol);
    pOrder->setOrderType(normalOrder->m_type);
    pOrder->m_openTime = normalOrder->m_openTime;
    pOrder->m_openPrice = normalOrder->m_openPrice;
    pOrder->m_closeTime = normalOrder->m_currTime;
    pOrder->m_closePrice = normalOrder->m_currPrice;
    pOrder->m_highPrice = normalOrder->m_highPrice;
    pOrder->m_lowPrice = normalOrder->m_lowPrice;
    pOrder->m_lots = normalOrder->m_lots;
    pOrder->m_stopLoss = normalOrder->m_stopLoss;
    pOrder->m_takeProfit = normalOrder->m_takeProfit;
    pOrder->m_leverageRate = normalOrder->m_leverageRate;
    pOrder->m_fee = normalOrder->m_fee;
    return pOrder;
}

HistoryOrder* OrderFactory::createHistoryOrder(PendingOrder *pendOrder)
{
    HistoryOrder *pOrder = new HistoryOrder();
    pOrder->m_id = pendOrder->m_id;
    strcpy(pOrder->m_symbol, pendOrder->m_symbol);
    pOrder->setOrderType(pendOrder->m_type);
    pOrder->m_openTime = pendOrder->m_openTime;
    pOrder->m_openPrice = pendOrder->m_openPrice;
    pOrder->m_closeTime = pendOrder->m_currTime;
    pOrder->m_closePrice = pendOrder->m_currPrice;
    pOrder->m_lots = pendOrder->m_lots;
    pOrder->m_stopLoss = pendOrder->m_stopLoss;
    pOrder->m_takeProfit = pendOrder->m_takeProfit;
    pOrder->m_leverageRate = pendOrder->m_leverageRate;
    pOrder->m_fee = pendOrder->m_fee;
    return pOrder;
}

void OrderFactory::changeOrderType(HistoryOrder *order, const char *strType)
{
    if (strcmp(strType, "buy") == 0) {
        order->setOrderType(OrderBase::ORDERBUY);
    }
    if (strcmp(strType, "sell") == 0) {
        order->setOrderType(OrderBase::ORDERSELL);
    }
    if (strcmp(strType, "buylimit") == 0) {
        order->setOrderType(OrderBase::ORDERBUYLIMIT);
    }
    if (strcmp(strType, "selllimit") == 0) {
        order->setOrderType(OrderBase::ORDERSELLLIMIT);
    }
}